__author__ = 'Soul'

from core.PageFetcher import *
from dboperater.DbOperatorFactory import *
import string

class C360AppPageFetcher(PageFetcher):
     def init(self, url, intent=False, recurse=False):
        factory = DbOperatorFactory()
        self.__db__ = factory.GetDbOperator('appdb')
        return PageFetcher.init(self, url, True)

     def analysis(self):
        if self.__html_src__:
            global urls_to_visit
            from lxml.html import fromstring
            parsed_html = fromstring(self.__html_src__)
            app_info_panel = parsed_html.xpath("//div[@id='app-info-panel']")
            if type(app_info_panel) == list:
                app_info_panel = app_info_panel[0]

            if app_info_panel:
                app_info = app_info_panel.xpath("div[@class='product btn_type1']/dl[@class='clearfix']/dd")
                if type(app_info) == list:
                    app_info = app_info[0]

                app_name = app_info.xpath("h2[@id='app-name']/span/@title")
                if type(app_name) == list: app_name = app_name[0]
                app_star = app_info.xpath("div[@class='pf']/span[@class='s-1 js-votepanel']/text()")
                if type(app_star) == list: app_star = app_star[0]

                app_pbl_date = ""
                app_pbl_ent = ""

                app_ext_info_list = app_info.xpath("div[@class='pf']/span[@class='s-3']")
                app_down_count = ''
                if len(app_ext_info_list) > 0:
                    app_down_count = app_ext_info_list[0].text
                app_size = ''
                if len(app_ext_info_list) > 1:
                    app_size = app_ext_info_list[1].text
                app_desc_div = parsed_html.xpath("//div[@id='sdesc']/div[@class='breif']")
                if type(app_desc_div) == list:
                    app_desc_div = app_desc_div[0]

                app_desc = ''
                if app_desc_div:
                    app_desc = app_desc_div.text

                app_src = "360"
                import time
                app_fetch_dt = (int)(time.time())

                app_record = {
                    'app_name': app_name,
                    'app_desc': app_desc,
                    'app_star': app_star,
                    'app_src': app_src,
                    'app_size': app_size,
                    'app_pbl_ent': app_pbl_ent,
                    'app_pbl_date': app_pbl_date,
                    'app_down_count': app_down_count,
                    'app_fetch_dt': app_fetch_dt
                }

                self.__db__.insert_record(app_record)